package com.excesys.exsecs.energy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.DimArea;
import com.excesys.exsecs.energy.service.DimAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 能耗维度：区域
 *
 * @author lxc
 * @date 2024-02-26 14:13:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dimarea" )
@Tag(description = "DimArea" , name = "能耗维度：区域管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DimAreaController {

    private final DimAreaService dimAreaService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dimArea 能耗维度：区域
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('twinops_dimArea_view')" )
    public R getDimAreaPage(@ParameterObject Page page, @ParameterObject DimArea dimArea) {
        LambdaQueryWrapper<DimArea> wrapper = Wrappers.lambdaQuery();
        return R.ok(dimAreaService.page(page, wrapper));
    }

	/**
	 * 获取部门树
	 * @return
	 */
	@Operation(summary = "获取部门树" , description = "获取部门树" )
	@GetMapping("/tree" )
	@PreAuthorize("@pms.hasPermission('twinops_dimArea_view')" )
	public R getTree(@ParameterObject DimArea dimArea) {
		return R.ok(dimAreaService.queryTree(dimArea));
	}


    /**
     * 通过id查询区域信息及区域内所有表计
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('twinops_dimArea_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(dimAreaService.getAreaById(id));
    }

    /**
     * 新增能耗维度：区域
     * @param dimArea 能耗维度：区域
     * @return R
     */
    @Operation(summary = "新增能耗维度：区域" , description = "新增能耗维度：区域" )
    @SysLog("新增能耗维度：区域" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimArea_add')" )
    public R save(@RequestBody DimArea dimArea) {
        return R.ok(dimAreaService.save(dimArea));
    }

	/**
	 * 关联表计保存
	 * @return R
	 */
	@Operation(summary = "关联表计保存" , description = "关联表计保存" )
	@SysLog("关联表计保存" )
	@PostMapping("/saveDevAreaMap")
	@PreAuthorize("@pms.hasPermission('twinops_dimArea_add')" )
	public R saveDevAreaMap(@RequestBody DimArea dimArea) {
		return R.ok(dimAreaService.saveDevAreaMap(dimArea));
	}

	/**
	 * 通过parentId查询关联的表计信息
	 * @param dimArea
	 * @return R
	 */
	@Operation(summary = "通过id查询部门信息及部门内所有表计" , description = "通过id查询部门信息及部门内所有表计" )
	@GetMapping("/meter" )
	@PreAuthorize("@pms.hasPermission('twinops_dimArea_view')" )
	public R getMetersById(@ParameterObject DimArea dimArea) {
		return R.ok(dimAreaService.getMetersById(dimArea));
	}

    /**
     * 修改能耗维度：区域
     * @param dimArea 能耗维度：区域
     * @return R
     */
    @Operation(summary = "修改能耗维度：区域" , description = "修改能耗维度：区域" )
    @SysLog("修改能耗维度：区域" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimArea_edit')" )
    public R updateById(@RequestBody DimArea dimArea) {
        return R.ok(dimAreaService.updateById(dimArea));
    }

    /**
     * 通过id删除能耗维度：区域
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除能耗维度：区域" , description = "通过id删除能耗维度：区域" )
    @SysLog("通过id删除能耗维度：区域" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimArea_del')" )
    public R removeAreaById(@RequestBody Long[] ids) {
        return R.ok(dimAreaService.removeAreaBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param dimArea 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('twinops_dimArea_export')" )
    public List<DimArea> export(DimArea dimArea,Long[] ids) {
        return dimAreaService.list(Wrappers.lambdaQuery(dimArea).in(ArrayUtil.isNotEmpty(ids), DimArea::getId, ids));
    }
}